package com.douqu.game.core.factory;

import com.douqu.game.core.entity.GameObject;
import com.douqu.game.core.entity.InitData;
import com.douqu.game.core.entity.Localization;
import com.douqu.game.core.util.LoadUtils;
import org.apache.log4j.Logger;

import java.io.IOException;
import java.io.InputStream;
import java.util.*;

/**
 * Author : Bean
 * 2017-07-06 16:13
 */
public class LoadFactory {

    static Logger logger = Logger.getLogger(LoadFactory.class);
    private final static String CONFIG_NAME = "config.properties";
    private final static String WORD_CONFIG = "data/Localization";


    private static Properties loadConfig(String configPath)
    {
        logger.info("【Load Config Start FileName=" + configPath + "】");
        Properties properties = new Properties();
        InputStream input =  null;
        try {
            input = LoadFactory.class.getClassLoader().getResourceAsStream(configPath);
            properties.load(input);

//            logger.info("Load Signal Config Start=====================================");
//
//            logger.info("Load Signal Config Success==============================");

        }catch (Exception e){
            e.printStackTrace();
        }finally {
            if(input != null){
                try {
                    input.close();
                } catch (IOException e) {
                    e.printStackTrace();
                    logger.error(e.getMessage());
                }
            }
        }

        return properties;
    }


    private final static String initDataUrl = "data/initData";
    /**
     * 加载初始数据配置
     */
    public static void loadInitData()
    {
        InputStream input = null;
        try {
            input = LoadFactory.class.getClassLoader().getResourceAsStream(initDataUrl);
            List<InitData> datas = LoadUtils.loadFileVariables(input, InitData.class);
            DataFactory.getInstance().setInitData(datas.get(0));
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            try {
                if(input != null){
                    input.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
                logger.error(e.getMessage());
            }
        }
    }



    public static void loadGameObject(String path, Class cls, int key)
    {
        InputStream input = null;
        try {
            input = LoadFactory.class.getClassLoader().getResourceAsStream(path);
            List<GameObject> datas = LoadUtils.loadFileVariables(input, cls);
            Map<Integer,GameObject> map = new HashMap<>();
            for(GameObject gameObject : datas)
            {
                gameObject.check();

                map.put(gameObject.id, gameObject);
            }

            DataFactory.getInstance().addData(key, datas, map);

            if(key == DataFactory.GOODS_PROP_KEY || key == DataFactory.GOODS_EQUIP_KEY)
            {
                List list = DataFactory.getInstance().getDataList(DataFactory.GOODS_KEY);
                if(list == null)
                {
                    DataFactory.getInstance().addData(DataFactory.GOODS_KEY, datas, map);
                }
                else
                {
                    list.addAll(datas);
                    Map<Integer,GameObject> goodsMap = DataFactory.getInstance().getDataMap(DataFactory.GOODS_KEY);
                    goodsMap.putAll(map);
                }
            }

            if(key == DataFactory.GRADE_KEY)
            {
                DataFactory.getInstance().MAX_GRADE = datas.get(datas.size()-1).getId();
            }

            logger.info("Load " + cls.getSimpleName() + " Success!");

        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            try {
                if(input != null){
                    input.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
                logger.error(e.getMessage());
            }
        }

    }

    public static void loadConfig()
    {
        logger.info("【Load Config Start FileName=" + CONFIG_NAME + "】");
        Properties properties = new Properties();
        InputStream input =  null;
        try {
            input = LoadFactory.class.getClassLoader().getResourceAsStream(CONFIG_NAME);
            properties.load(input);

            ConfigFactory.LANGUAGE = properties.get("LANGUAGE").toString().trim();
            ConfigFactory.SERVER_IP = properties.get("SERVER_IP").toString().trim();
            ConfigFactory.MAIN_SERVER_TCP_PORT = Integer.parseInt(properties.get("MAIN_SERVER_TCP_PORT").toString().trim());
            ConfigFactory.BATTLE_SERVER_PORT_START = Integer.parseInt(properties.get("BATTLE_SERVER_PORT_START").toString().trim());

            ConfigFactory.TENCENT_COS_APP_ID = Long.parseLong(properties.get("TENCENT_COS_APP_ID").toString().trim());
            ConfigFactory.TENCENT_COS_SECRET_ID = properties.get("TENCENT_COS_SECRET_ID").toString().trim();
            ConfigFactory.TENCENT_COS_SECRET_KEY = properties.get("TENCENT_COS_SECRET_KEY").toString().trim();
            ConfigFactory.TENCENT_COS_BUCKET = properties.get("TENCENT_COS_BUCKET").toString().trim();

            ConfigFactory.EASEMOB_API_HTTP_SCHEMA = properties.get("EASEMOB_API_HTTP_SCHEMA").toString().trim();
            ConfigFactory.EASEMOB_API_SERVER_HOST = properties.get("EASEMOB_API_SERVER_HOST").toString().trim();
            ConfigFactory.EASEMOB_DEFAULT_PASSWORD = properties.get("EASEMOB_DEFAULT_PASSWORD").toString().trim();
            ConfigFactory.EASEMOB_USER_ROLE_APPADMIN = properties.get("EASEMOB_USER_ROLE_APPADMIN").toString().trim();
            ConfigFactory.EASEMOB_APPKEY = properties.get("EASEMOB_APPKEY").toString().trim();
            ConfigFactory.EASEMOB_APP_CLIENT_ID = properties.get("EASEMOB_APP_CLIENT_ID").toString().trim();
            ConfigFactory.EASEMOB_APP_CLIENT_SECRET = properties.get("EASEMOB_APP_CLIENT_SECRET").toString().trim();

            ConfigFactory.PHONE_MESSAGE_SIGN = properties.get("PHONE_MESSAGE_SIGN").toString().trim();
            ConfigFactory.PHONE_MESSAGE_CONTENT = properties.get("PHONE_MESSAGE_CONTENT").toString().trim();
            ConfigFactory.PHONE_MESSAGE_URL = properties.get("PHONE_MESSAGE_URL").toString().trim();
            ConfigFactory.PHONE_MESSAGE_CAPTCHA = properties.get("PHONE_MESSAGE_CAPTCHA").toString().trim();

            ConfigFactory.WECHAT_USERINFO_URL = properties.get("WECHAT_USERINFO_URL").toString().trim();
            ConfigFactory.QQ_USERINFO_URL = properties.get("QQ_USERINFO_URL").toString().trim();

            ConfigFactory.XIAOMI_VERIFY_URL = properties.get("XIAOMI_VERIFY_URL").toString().trim();
            ConfigFactory.XIAOMI_APP_ID = properties.get("XIAOMI_APP_ID").toString().trim();
            ConfigFactory.XIAOMI_APP_SECRET = properties.get("XIAOMI_APP_SECRET").toString().trim();

            ConfigFactory.DEFAULT_USER_PASSWORD = properties.get("DEFAULT_USER_PASSWORD").toString().trim();

            ConfigFactory.CLOSE_SERVER_KEY = properties.get("CLOSE_SERVER_KEY").toString().trim();

            logger.info("LANGUAGE------------------【" + ConfigFactory.LANGUAGE + "】");
            logger.info("SERVER_IP-----------------【" + ConfigFactory.SERVER_IP + "】");
            logger.info("MAIN_SERVER_TCP_PORT------【" + ConfigFactory.MAIN_SERVER_TCP_PORT + "】");
            logger.info("BATTLE_SERVER_PORT_START--【" + ConfigFactory.BATTLE_SERVER_PORT_START + "】");

            logger.info("TENCENT_COS_APP_ID--------【" + ConfigFactory.TENCENT_COS_APP_ID + "】");
            logger.info("TENCENT_COS_SECRET_ID-----【" + ConfigFactory.TENCENT_COS_SECRET_ID + "】");
            logger.info("TENCENT_COS_SECRET_KEY----【" + ConfigFactory.TENCENT_COS_SECRET_KEY + "】");
            logger.info("TENCENT_COS_BUCKET--------【" + ConfigFactory.TENCENT_COS_BUCKET + "】");

            logger.info("PHONE_MESSAGE_SIGN--------【" + ConfigFactory.PHONE_MESSAGE_SIGN + "】");
            logger.info("PHONE_MESSAGE_CONTENT-----【" + ConfigFactory.PHONE_MESSAGE_CONTENT + "】");
            logger.info("PHONE_MESSAGE_URL---------【" + ConfigFactory.PHONE_MESSAGE_URL + "】");
            logger.info("PHONE_MESSAGE_CAPTCHA-----【" + ConfigFactory.PHONE_MESSAGE_CAPTCHA + "】");

            logger.info("WECHAT_USERINFO_URL-------【" + ConfigFactory.WECHAT_USERINFO_URL + "】");
            logger.info("QQ_USERINFO_URL-----------【" + ConfigFactory.QQ_USERINFO_URL + "】");

            logger.info("XIAOMI_VERIFY_URL---------【" + ConfigFactory.XIAOMI_VERIFY_URL + "】");
            logger.info("XIAOMI_APP_ID-------------【" + ConfigFactory.XIAOMI_APP_ID + "】");
            logger.info("XIAOMI_APP_SECRET---------【" + ConfigFactory.XIAOMI_APP_SECRET + "】");

            logger.info("DEFAULT_USER_PASSWORD-----【" + ConfigFactory.DEFAULT_USER_PASSWORD + "】");

            logger.info("CLOSE_KEY-----------------【" + ConfigFactory.CLOSE_SERVER_KEY + "】");
            logger.info("Load Config Success==============================");

        }catch (Exception e){
            e.printStackTrace();
        }finally {
            if(input != null){
                try {
                    input.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }


    public static void loadWord()
    {
        InputStream input = null;
        try {
            logger.info("【Load Word Start FileName=" + WORD_CONFIG + "】");

            input = LoadFactory.class.getClassLoader().getResourceAsStream(WORD_CONFIG);
            List<Localization> datas = LoadUtils.loadFileVariables(input, Localization.class);
            Map<Integer,Localization> map = new HashMap<>();
            for(Localization gameObject : datas)
            {
                if(ConfigFactory.LANGUAGE.equals(ConstantFactory.LANGUAGE_CHINESE))
                {
                    DataFactory.getInstance().addWord(gameObject.key, gameObject.Chinese);
                }
                else if(ConfigFactory.LANGUAGE.equals(ConstantFactory.LANGUAGE_CHINESETRADITIONAL))
                {
                    DataFactory.getInstance().addWord(gameObject.key, gameObject.ChineseTraditional);
                }
                else if(ConfigFactory.LANGUAGE.equals(ConstantFactory.LANGUAGE_ENGLISH))
                {
                    DataFactory.getInstance().addWord(gameObject.key, gameObject.English);
                }
            }
            logger.info("Load Word Success=================================");
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            try {
                if(input != null){
                    input.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
                logger.error(e.getMessage());
            }
        }
    }

    public static void main(String[] args) {
//        LoadFactory.loadGameObject("data/soldierLv", CardLv.class, DataFactory.SOLDIER_LV_KEY);

    }
}
